<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>FeedReader</title>
	<style type="text/css">
	body
	{
		margin:4px;
		padding:0px;
		border:1px solid #999;
		background-color:#000;
		font-family:'Courier New', monospace;
		font-size:0.8em;
		color:rgb(145,208,133);
		overflow:auto;
	}

	ul
	{
		margin:0px;
		padding:0px;
	}

	li
	{
		margin:0px;
		padding:0px;
	}

	a
	{
		margin:2px;
		padding:4px;
		text-decoration:none;
		font-family:'Courier New', monospace;
		color:rgb(145,208,133);
	}

	a:hover
	{
		color:rgb(145,255,133);
	}
	</style>
	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript">
	// feed url
	var feedUrl = false;

	// feed entry count
	var count = false;

	// width and height of the gadget
	var width = false;
	var height = false;

	// max title length
	var maxTitleLength = false;

	// time waiting between requests in seconds
	var requestSleep = false;


	function initGadget()
	{
		$('#gadgetContent').html('Loading ...');

		loadSettings();

		$('body').css('width', width);
		$('body').css('height', height);

		getFeed();

		if(feedInterval)
		{
			clearInterval(feedInterval);
		}

		feedInterval = setInterval('getFeed()', requestSleep * 1000);
	}

	function getFeed()
	{
		var nocache = 'nocache=' + (new Date().getTime());
		var url = feedUrl + (feedUrl.indexOf('?') == -1 ? '?' : '&') + nocache;

		$.get(url, function(response){

			var html = '<ul>';
			var realCount = 0;

			switch(true)
			{
				case isAtom(response):

					var entries = response.getElementsByTagName('entry');

					for(var i = 0; i < entries.length; i++)
					{
						var childs = entries[i].childNodes;
						var title  = false;
						var link   = false;

						for(var j = 0; j < childs.length && realCount < count; j++)
						{
							if(childs[j].nodeType == 1)
							{
								if(childs[j].nodeName == 'title')
								{
									title = childs[j].text;
								}

								if(childs[j].nodeName == 'link')
								{
									link = childs[j].getAttribute('href');
								}
							}

							if(title !== false && link !== false)
							{
								html+= '<li><a href="' + link + '" title="' + title + '">' + (title.length <= maxTitleLength ? title : title.substring(0, maxTitleLength - 3) + '...') + '</a></li>';

								realCount++;

								break;
							}
						}
					}

					break;

				case isRss(response):

					var items = response.getElementsByTagName('item');

					for(var i = 0; i < items.length; i++)
					{
						var childs = items[i].childNodes;
						var title  = false;
						var link   = false;

						for(var j = 0; j < childs.length && realCount < count; j++)
						{
							if(childs[j].nodeType == 1)
							{
								if(childs[j].nodeName == 'title')
								{
									title = childs[j].text;
								}

								if(childs[j].nodeName == 'link')
								{
									link = childs[j].text;
								}
							}

							if(title !== false && link !== false)
							{
								html+= '<li><a href="' + link + '" title="' + title + '">' + (title.length <= maxTitleLength ? title : title.substring(0, maxTitleLength - 3) + '...') + '</a></li>';

								realCount++;

								break;
							}
						}
					}

					break;

				default:

					html+= '<li>Invalid Atom or RSS feed</li>';

					break;
			}

			html+= '</ul>';

			$('#gadgetContent').html(html);

		});
	}

	function isAtom(response)
	{
		return response.getElementsByTagName('feed').length == 1 && response.getElementsByTagName('entry').length > 0;
	}

	function isRss(response)
	{
		return response.getElementsByTagName('rss').length == 1 && response.getElementsByTagName('item').length > 0;
	}

	function loadSettings()
	{
		var settingsFeedUrl = System.Gadget.Settings.readString('settingsFeedUrl');
		var settingsCount = System.Gadget.Settings.readString('settingsCount');
		var settingsWidth = System.Gadget.Settings.readString('settingsWidth');
		var settingsHeight = System.Gadget.Settings.readString('settingsHeight');
		var settingsMaxTitleLength = parseInt(System.Gadget.Settings.readString('settingsMaxTitleLength'));
		var settingsRequestSleep = parseInt(System.Gadget.Settings.readString('settingsRequestSleep'));

		if(settingsFeedUrl)
		{
			feedUrl = settingsFeedUrl;
		}
		else
		{
			feedUrl = 'http://news.google.com/news?topic=t&output=atom';
		}

		if(settingsCount)
		{
			count = settingsCount;
		}
		else
		{
			count = 20;
		}

		if(settingsWidth)
		{
			width = settingsWidth;
		}
		else
		{
			width = 460;
		}

		if(settingsHeight)
		{
			height = settingsHeight;
		}
		else
		{
			height = 340;
		}

		if(settingsMaxTitleLength > 0)
		{
			maxTitleLength = settingsMaxTitleLength;
		}
		else
		{
			maxTitleLength = 54;
		}

		if(settingsRequestSleep > 0)
		{
			requestSleep = settingsRequestSleep;
		}
		else
		{
			requestSleep = 60;
		}
	}

	function settingsClosed(event)
	{
		if(event.closeAction == event.Action.commit)
		{
			initGadget();
		}
		else if (event.closeAction == event.Action.cancel)
		{
		}
	}

	jQuery.support.cors = true;

	System.Gadget.settingsUI = "Settings.html";
	System.Gadget.onSettingsClosed = settingsClosed;

	var feedInterval = false;

	$(document).ready(function(){

		initGadget();

	});
	</script>
</head>
<body>
<div id="gadgetContent">Loading ...</div>
</body>
</html>

